﻿using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace UniversityStudentManagemnetSystem.DAO.Gateway
{
    public class Gateway
    {
        private SqlConnection connection;
        private SqlCommand command;
        private SqlDataReader dataReader; 

        protected enum Rows
        {
            oneRowAffected = 1,
            noRowAffected = 0,
            oneRowSelected = 1,
            noRowSeleced = 0,
            oneRowCounted = 1,
            noRowCounted = 0
        }

        protected int nbrOfAffectedRows = 0;
        protected int nbrOfSelectedRows = 0;
        protected int nbrOfTableRows = 0;
        protected string queryString;

        public  Gateway()
        {
            connection = new SqlConnection(GetConnectionString());
            command = new SqlCommand();
        }

        public string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["UniversitySystemConnection"].ConnectionString;
        }

        public SqlConnection Connection
        {
            get { return connection; }
        }

        public SqlCommand Command
        {
            get
            {
                command.Connection = connection;
                return command;
            }
            
        }

        public SqlDataReader DataReader
        {
            get { return dataReader; }
            set { dataReader = value; }
        }


        public string GetQueryString(SqlCommand command)
        {
            string query = command.CommandText;

            foreach (SqlParameter parameter in command.Parameters)
            {
                string key = parameter.ParameterName;
                string value = parameter.Value.ToString();

                if (parameter.SqlDbType.Equals(SqlDbType.VarChar))
                {
                    value = "'" + parameter.Value + "'";
                }

                query = query.Replace(key, value);
            }

            return query;
        }
    }
}